package fr.uniscol.siecle.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Departement implements Serializable, DonneeSiecle {

	private Academie academie = null;
	
	private String code = null;
	
	private String code_men = null;
	
	private String nom = "";

	private List<Commune> communes = null;
	
	public Departement() {
	}
	
	public Departement(String code, String nom) {
		super();
		this.code = code;
		this.nom = nom;
	}

	public Academie getAcademie() {
		return academie;
	}

	public void setAcademie(Academie academie) {
		this.academie = academie;
	}

	public String getCode() {
		if(code == null){
			code = "";
		}
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getNom() {
		if(nom == null){
			nom = "";
		}
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public List<Commune> getCommunes() {
		if(communes == null){
			communes = new ArrayList<Commune>();
		}
		return communes;
	}

	public String getCode_men() {
		return code_men;
	}

	public void setCode_men(String code_men) {
		this.code_men = code_men;
	}

	public void setCommunes(List<Commune> communes) {
		this.communes = communes;
	}
	
	public Commune getCommune(String code){
		for(Commune c : getCommunes()){
			if(c.getCode().equals(code)){
				return c;
			}
		}
		return null;
	}
	
	public void ajouterCommune(Commune c){
		if(!getCommunes().contains(c)){
			getCommunes().add(c);
		}
		if(c.getDepartement() == null || !c.getDepartement().equals(this)){
			c.setDepartement(this);
		}
	}

	@Override
	public boolean equals(Object obj) {
		Departement d = (Departement)obj;
		return getCode().equals(d.getCode());
	}

	@Override
	public int hashCode() {
		if(getCode() == null || getCode().equals("")){
			return super.hashCode();
		}
		return Integer.parseInt(getCode());
	}
}
